package com.zit.java.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zit.java.pojo.TeacherSalaryInput;
import com.zit.java.service.teacher.TeacherService;
import com.zit.java.util.object.AjaxDone;
import com.zit.java.util.object.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("teacher")
public class TeacherController {


    @Autowired
    private TeacherService servceDk;

    @RequestMapping("/teacherSalaryPag")
    public String toTeacherSalaryPag(){
        return "admin/teacherSalaryInput";
    }


    @RequestMapping("/queryAllTeacherData")
    @ResponseBody
    public TableData allTableData(@RequestParam("page") Integer page,@RequestParam("limit")Integer limit){
         PageHelper.startPage(page, limit);
        TableData<TeacherSalaryInput> data = new TableData<>();
         List<TeacherSalaryInput> restList  =  servceDk.queryAllData();
        PageInfo<TeacherSalaryInput> ifo = new PageInfo<>(restList);
         data.setCode(0);
         data.setData(ifo.getList());
         data.setMsg("Ok");
         data.setCount(ifo.getTotal());
        return data;
    }

    @RequestMapping("/toSaveTeacherSalaryMsg")
    public String toSaveTeacherSalaryPag(){
        return "admin/addTeacherSalary";
    }

    @RequestMapping("/saveTeacherSalaryData")
    @ResponseBody
    public AjaxDone saveTeacherSalaryData(TeacherSalaryInput jsonObj){
        int b = servceDk.saveObj(jsonObj);
        if (b>0){
            return new AjaxDone(200,"添加成功");
        }
        return new AjaxDone(300,"添加失败");
    }

    /**
     * 到编辑教师薪资页面
     */
    @RequestMapping("/toTeacherPag")
    public String toTeacherPag(@RequestParam("id") Integer id, Model model){
       TeacherSalaryInput tsi =  servceDk.queryDataById(id);
       model.addAttribute("teacher",tsi);
        return "admin/editTeacherSalary";
    }

    @RequestMapping("/updTeacherMsg")
    @ResponseBody
    public AjaxDone updateTeacherMsg(TeacherSalaryInput teacherSalaryInput){
       int rstMsg =  servceDk.updTeacherMsg(teacherSalaryInput);
       if (rstMsg>0){
           return new AjaxDone(200,"修改成功");
       }
       return new AjaxDone(300,"修改失败");
    }

    @RequestMapping("/queryTeacherSalary")
    public String queryTeacherSalary(){
        return "personalsalary/querySalary";
    }

    @RequestMapping("showTeacherSalary")
    @ResponseBody
    public TableData<TeacherSalaryInput> queryTeacher(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        TableData<TeacherSalaryInput> tb = new TableData<>();
        List<TeacherSalaryInput> tsi= servceDk.queryAllData();
        PageInfo<List<TeacherSalaryInput>> info = new PageInfo(tsi);
        tb.setData(info.getList());
        tb.setMsg("ok");
        tb.setCount(info.getTotal());
        tb.setCode(0);
        return tb;
    }

    @RequestMapping("queryByTeacherName")
    @ResponseBody
    public TableData<TeacherSalaryInput> queryByTeacherName(@RequestParam("username") String username){
        TableData<TeacherSalaryInput> tb = new TableData<>();
        List<TeacherSalaryInput> tsi= servceDk.queryQueryDataByName(username);
        tb.setData(tsi);
        tb.setMsg("ok");
        tb.setCount(tsi.size());
        tb.setCode(0);
        return tb;
    }
}
